using System;
using System.Collections.Generic;
using System.Text;
using Decav.Gatsb.Mobile;

namespace Decav.Gatsb.Application.CommandInterpreters
{
    /// <summary>
    /// Produces smiley faces to make Gatsb users happier about their decision to partake in Gatsb.  Alright fine, its 
    /// an easter egg.
    /// </summary>
    public class SmileyFaceCommandInterpreter : ICommandInterpreter
    {
        #region ICommandInterpreter Members

        public void Execute(CommandInterpreterContext context)
        {
            if (!context.MessageHasText)
                return;

            MmsMessage mediaMessage = context.InputMessage as MmsMessage;
            if (mediaMessage != null && mediaMessage.Attachments.Count > 0)
                return;

            string detail = null;
            if (!context.FindCommand(_commandPrefixes, out detail))
                return;

            if (!string.IsNullOrEmpty(detail))
                return;

            string line = context.GetFirstLine();
            string outputMessage = null;

            switch (line)
            {
                case ":-)":
                case ":)":
                    outputMessage = GetHappy();
                    break;

                case ":-(":
                case ":(":
                    outputMessage = GetSad();
                    break;
            }

            outputMessage = outputMessage.Replace("#NAME#", GatsbApplication.Current.CurrentUser.Name.FirstName);
            context.SendResponse(outputMessage);
            context.EndParse();
        }

        private string GetHappy()
        {
            lock (_rand)
            {
                int message = _rand.Next(0, _happyMessages.Length-1);
                return _happyMessages[message];
            }
        }

        private string GetSad()
        {
            lock (_rand)
            {
                int message = _rand.Next(0, _sadMessages.Length - 1);
                return _sadMessages[message];
            }
        }

        private static readonly Random _rand = new Random();
        private readonly string[] _commandPrefixes = new string[] {
            ":-)", ":)",
            ":(", ":-(" };

        private readonly string[] _happyMessages = new string[]
            {
                ":-) *bite*",
                ":-)",
                "If you're lucky ;-)",
                "Congrats #NAME#, you found an easter egg!\r\n\r\nIsn't it creepy Gatsb is talking to you? :-(",
                ":-) yourself.",
                "That's exciting!",
                "The Great Gatsby:  A novel written by F. Scott Fitzgerald\r\n\r\nGatsby was best known for his extravagant parties :-)",
                "Gatsb - By Andre de Cavaignac\r\n\r\nSpecial thanks to: Jocelyn Chambers, Elyse Romano, Jon Corbett, Rachel Guim, Luke Flemmer, Daniel Chait, Josh Einstein"
            };

        private readonly string[] _sadMessages = new string[]
            {
                "You have no friends! ;)",
                "Cheer up!",
                "<3 from NYC :)",
                "Let Gatsb help you find a better party :)",
                "What's wrong #NAME#?",
                "Yeah that girl's a bitch, why don't you go home and browse Gatsb? ;-)"
            };

        #endregion
    }
}
